package zz.demo.springCloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import feign.Feign;
import feign.RequestTemplate;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.support.SpringDecoder;
import org.springframework.web.bind.annotation.*;
import zz.demo.springCloud.entity.User;
import zz.demo.springCloud.feignClient.UserFeignClient;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.security.Principal;

/**
 * Created by zhangzheng on 2017-6-5.
 */
@RestController

public class OAController {
//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/add/{a}/{b}")
    public Integer add(@PathVariable("a")Integer a,@PathVariable("b")Integer b){
        return this.userFeignClient.add(a,b);
    }

    @GetMapping("/hello")
    public String hello(){
        return "hello world!";
    }

    @GetMapping("/user/{id}")
//    @HystrixCommand(fallbackMethod = "getUserFallBack")
    public User getId(@PathVariable("id")Long id){
        return this.userFeignClient.getUser(id);
    }

    @GetMapping("/addUser")
    public User addUser(User user){
        return this.userFeignClient.addUser(user);
    }

    public User getUserFallBack(Long id){
        return new User(id);
    }

    @GetMapping("/session/{name}")
    public String getSession(HttpSession httpSession,@PathVariable("name")String name){
        String value= (String) httpSession.getAttribute(name);
        System.out.println(httpSession.toString());
        return value;
    }

    @PostMapping("/login")
    public Principal login(Principal user){
        return user ;
    }

    @GetMapping("/user")
    public Principal user(Principal user) {
        return user ;
    }
//
//    @GetMapping("/")
//    public String main(Principal user){
//        return "OA:HELLO" + user.getName() ;
//    }

    @GetMapping("/error")
    public String error(){
        return "error" ;
    }
}
